package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.Sm;
import com.stylefeng.guns.modular.system.service.ISmService;
import com.stylefeng.guns.modular.system.warpper.SmWarpper;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-05-23 10:30:36
 */
@Controller
@RequestMapping("/sm")
public class SmController extends BaseController {

    private String PREFIX = "/system/sm/";

    @Autowired
    private ISmService smService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sm.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/sm_add")
    public String smAdd() {
        return PREFIX + "sm_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/sm_update/{smId}")
    public String smUpdate(@PathVariable Integer smId, Model model) {
        Sm sm = smService.selectById(smId);
        model.addAttribute("item",sm);
        LogObjectHolder.me().set(sm);
        return PREFIX + "sm_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name) {
    	Page<Sm> page = new PageFactory<Sm>().defaultPage();
    	List<Map<String, Object>> gdhs = smService.selectMaps(name,page);
    	page.setRecords((List<Sm>) new SmWarpper(gdhs).warp());
        return super.packForBT(page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Sm sm) {
        smService.insert(sm);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer smId) {
        smService.deleteById(smId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Sm sm) {
        smService.updateById(sm);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{smId}")
    @ResponseBody
    public Object detail(@PathVariable("smId") Integer smId) {
        return smService.selectById(smId);
    }
}
